package com.ruoyi.testfunction.utils;

import com.alibaba.fastjson2.JSONObject;
import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author FF998
 */
public class ApiUtil {

    private static final OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .build();

//    private static final OkHttpClient client = new OkHttpClient().newBuilder().build();
    /**
     * 笑话API
     *
     * @param quantity 数量
     * @return String
     * @throws IOException
     */
    public static String JokeAPI(int quantity) throws IOException {
        String urlStr = "https://eolink.o.apispace.com/xhdq/common/joke/getJokesByRandom";

//        JSONObject bodyParams = new JSONObject();
//        bodyParams.put("pageSize", quantity);
//
//        RequestBody body = RequestBody.create(type, bodyParams.toString());


        Headers.Builder builder = new Headers.Builder()
                .add("X-APISpace-Token", "eo69pr11lq6atic5gc9g003bi4bp0rto")
                .add("Content-Type", "application/x-www-form-urlencoded");
        Headers headers = builder.build();

        MediaType type = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(type,"pageSize="+quantity);

        Request build = new Request.Builder().url(urlStr).headers(headers).post(body).build();
        Response response = client.newCall(build).execute();
        int status = response.code();
        return response.body().string();
        /**
         * {
         *     "statusCode": "000000",
         *     "desc": "查询成功",
         *     "result": [
         *         {
         *             "id": 1121,
         *             "content": "一次我和我男友挤公交，我穿的白色的裙子，姨妈不期而至，于是我向男友投出寻求帮助的眼神，本想让他找东西帮我挡挡，结果...他抱起我喊着，媳妇儿，坚持住！！我们马上去医院，这个孩子一定要保住呀！！尼玛……",
         *             "updateTime": "2017-03-29 17:33:50"
         *         }
         *     ]
         * }
         */
    }

    /**
     * 天气预报API
     *
     * @param code 城市编码
     * @return String
     * @throws IOException
     */
    public static String WeatherPlus(String code) throws IOException {
        String urlStr = "https://eolink.o.apispace.com/456456/weather/v001/now?areacode=" + code;

        Headers.Builder builder = new Headers.Builder()
                .add("X-APISpace-Token", "eo69pr11lq6atic5gc9g003bi4bp0rto");
        Headers headers = builder.build();


        Request request = new Request.Builder().url(urlStr).headers(headers).build();
        Response response = client.newCall(request).execute();
        int status = response.code();
        return response.body().string();
        /**
         * {
         *     "status": 0,
         *     "result": {
         *         "location": {
         *             "areacode": "101190108",
         *             "name": "玄武",
         *             "country": "中国",
         *             "path": "玄武,南京市,江苏省,中国"
         *         },
         *         "realtime": {
         *             "text": "晴",
         *             "code": "00",
         *             "temp": 16.7,
         *             "feels_like": 14,
         *             "rh": 26,
         *             "wind_class": "3级",
         *             "wind_speed": 4.1,
         *             "wind_dir": "北风",
         *             "wind_angle": 21,
         *             "prec": 0.0,
         *             "prec_time": "2024-03-12 14:00:00",
         *             "clouds": 0,
         *             "vis": 8200,
         *             "pressure": 1019,
         *             "dew": -3,
         *             "uv": 5,
         *             "weight": 7,
         *             "brief": "凉而干燥",
         *             "detail": "天干物燥，记得保湿补水哦"
         *         },
         *         "last_update": "2024-03-12 14:36"
         *     }
         * }
         */
    }
}
